/**
 * 
 */
package jvs.vfs.opt.contact;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;

/**
 * @author qiangli
 * 
 */
public final class ContactUtils {
	public static void loadMailLists(Context ctx, List<String> lists) {
		ContactAdapter ca = ContactAdapter.getAdapter();
		lists.clear();
		//
		HashSet<String> set = new HashSet<String>();
		ca.loadListNames(ctx, set);
		lists.addAll(set);
		//
		Collections.sort(lists);
	}

	/**
	 * 
	 * @param ctx
	 * @param name
	 * @return sorted list of email addresses
	 */
	public static String[] getEmail(Context ctx, String name) {
		ContactAdapter ca = ContactAdapter.getAdapter();
		String[] emails = ca.getEmails(ctx, name);
		if (emails == null) {
			return new String[0];
		}
		Arrays.sort(emails); //
		return emails;
	}

	public static void loadEmailContact(Context ctx, List<ContactInfo> contacts) {
		ContactAdapter ca = ContactAdapter.getAdapter();
		contacts.clear();
		HashSet<ContactInfo> set = new HashSet<ContactInfo>();
		//
		ca.loadEmailContacts(ctx, set);
		contacts.addAll(set);
	}

	public static void loadPhoneContact(Context ctx,
			List<ContactInfo> contacts, int idx, String by) {
		ContactAdapter ca = ContactAdapter.getAdapter();
		contacts.clear();
		ca.loadPhoneContacts(ctx, contacts, idx, by);
	}

	/**
	 * 
	 * @param context
	 * @param name
	 * @return
	 */
	public static long getId(Context ctx, String name) {
		ContactAdapter ca = ContactAdapter.getAdapter();
		return ca.getId(ctx, name);
	}

	public static boolean hasEmail(Context ctx, String email) {
		ContactAdapter ca = ContactAdapter.getAdapter();
		return ca.hasEmail(ctx, email);
	}

	public static void loadPhoneFavorite(Context ctx,
			List<ContactInfo> contacts, int idx, String by) {
		ContactAdapter ca = ContactAdapter.getAdapter();
		contacts.clear();
		//
		ca.loadPhoneFavorite(ctx, contacts, idx, by);
	}

	public static void loadEmailFavorite(Context ctx, List<ContactInfo> contacts) {
		ContactAdapter ca = ContactAdapter.getAdapter();
		contacts.clear();

		HashSet<ContactInfo> set = new HashSet<ContactInfo>();
		ca.loadEmailFavorite(ctx, set);
		contacts.addAll(set);
	}

	public static Intent createCallIntent(String number) {
		// new Intent(Intent.ACTION_VIEW, Uri.parse("tel:"+ number));
		// Intent.ACTION_CALL_PRIVILEGED
		Intent i = new Intent(Intent.ACTION_CALL, Uri.fromParts("tel",
				number, null));
		return i;
	}

	public static Intent createTextIntent(String number) {
		Intent i = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
				"sms", number, null));
		return i;
	}

	public static Intent createEmailIntent(String[] emails) {
		Intent i = new Intent();
		i.setAction(Intent.ACTION_SEND);
		i.setType("message/rfc822");
		// email
		i.putExtra(Intent.EXTRA_EMAIL, emails);
		// i.putExtra(Intent.EXTRA_SUBJECT, "");
		// i.putExtra(Intent.EXTRA_TEXT, "");
		//
		return i;
	}
	

	public static Bitmap loadPhoto(Context ctx, long id) {
		ContactAdapter ca = ContactAdapter.getAdapter();
		//android.R.drawable.sym_contact_card;
		return ca.loadPhoto(ctx, id);
	}
}